package com.backend.springboot_project_demo.service;

import com.backend.springboot_project_demo.entity.TestTestTest;
import com.backend.springboot_project_demo.entity.UserTransfer;
import com.backend.springboot_project_demo.mappers.UserMapper;
import com.backend.springboot_project_demo.entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SearchUserService implements UserDetailsService {
    private @Resource UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TestTestTest testTestTest = new TestTestTest();
        testTestTest.setId("");
        System.out.println(testTestTest);
        User user = this.findUserById(username);
        if (Objects.isNull(user)) {
            throw new RuntimeException("查无此人");
        } else {
            return user.toUserTransfer();
        }
    }

    public User findUserById(String id) {
        return userMapper.findUserById(id);
    }

    public List<User> findUserByName(String username) {
        return userMapper.findUserByUsername(username);
    }

    public List<UserTransfer> getUsers() {
        return userMapper.findUsers().stream().map(User::toUserTransfer).collect(Collectors.toList());
    }

    public List<UserTransfer> getUsersByPage(UserTransfer userTransfer) {
        return userMapper.getUsersByPage(userTransfer).stream().map(User::toUserTransfer).collect(Collectors.toList());
    }
}
